import paramiko


def saas_update_start(route='/saas-shyd-strategy'):
    hostname = '192.168.2.109'
    port = 22
    username = 'root'
    password = 'root6yhn^YHN'

    # 创建SSH客户端对象
    client = paramiko.SSHClient()

    # 允许连接不在know_hosts文件中的主机
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    client.connect(hostname, port, username, password)
    if route == '/saas-shyd-strategy':
        stdin1, stdout1, stderr1 = client.exec_command(
            f'cp /saas/saas-application/saas-app-service-exec.jar {route}/saas-application/shyd-strategy-app-service-exec.jar')
        error = stderr1.read().decode('utf-8')
        output = stdout1.read().decode('utf-8')
        if error:
            print("app_service服务拷贝失败")
        else:
            print(output)
            print("app_service服务拷贝成功")
        stdin2, stdout2, stderr2 = client.exec_command(
            f'cp /saas/saas-application/saas-oss-portal-exec.jar {route}/saas-application/shyd-strategy-oss-portal-exec.jar')
        error = stderr2.read().decode('utf-8')
        output = stdout2.read().decode('utf-8')
        if error:
            print("oss服务拷贝失败")
        else:
            print(output)
            print("oss服务拷贝成功")
        stdin3, stdout3, stderr3 = client.exec_command(
            f'cp /saas/saas-application/saas-userportal-exec.jar {route}/saas-application/shyd-strategy-userportal-exec.jar')
        error = stderr3.read().decode('utf-8')
        output = stdout3.read().decode('utf-8')
        if error:
            print("user服务拷贝失败")
        else:
            print(output)
            print("user服务拷贝成功")
    elif route == '/saas-shyd':
        stdin1, stdout1, stderr1 = client.exec_command(
            f'cp /saas/saas-application/saas-app-service-exec.jar {route}/saas-application/shyd-app-service-exec.jar')
        error = stderr1.read().decode('utf-8')
        output = stdout1.read().decode('utf-8')
        if error:
            print("app_service服务拷贝失败")
        else:
            print(output)
            print("app_service服务拷贝成功")
        stdin2, stdout2, stderr2 = client.exec_command(
            f'cp /saas/saas-application/saas-oss-portal-exec.jar {route}/saas-application/shyd-oss-portal-exec.jar')
        error = stderr2.read().decode('utf-8')
        output = stdout2.read().decode('utf-8')
        if error:
            print("oss服务拷贝失败")
        else:
            print(output)
            print("oss服务拷贝成功")
        stdin3, stdout3, stderr3 = client.exec_command(
            f'cp /saas/saas-application/saas-userportal-exec.jar {route}/saas-application/shyd-userportal-exec.jar')
        error = stderr3.read().decode('utf-8')
        output = stdout3.read().decode('utf-8')
        if error:
            print(error)
        else:
            print(output)
            print("user服务拷贝成功")
    print('jar包复制转移成功')
    # print('开始执行service重启')
    # stdin1, stdout1, stderr1 = client.exec_command(
    #     f'.{route}/saas-application/servicestartup.sh restart')
    # error = stderr1.read().decode('utf-8')
    # output = stdout1.read().decode('utf-8')
    # if error:
    #     print(error)
    # else:
    #     print(output)
    # print('开始执行oss重启')
    # stdin2, stdout2, stderr2 = client.exec_command(
    #     f'.{route}/saas-application/ossstartup.sh restart')
    # error = stderr2.read().decode('utf-8')
    # output = stdout2.read().decode('utf-8')
    # if error:
    #     print(error)
    # else:
    #     print(output)
    # print('开始执行user重启')
    # stdin3, stdout3, stderr3 = client.exec_command(
    #     f'.{route}/saas-application/userstartup.sh restart')
    # error = stderr3.read().decode('utf-8')
    # output = stdout3.read().decode('utf-8')
    # if error:
    #     print(error)
    # else:
    #     print(output)


if __name__ == '__main__':
    saas_update_start()
